<template>
	<view class="table">
			<!-- <view style="text-align: center;">总任务数{{dataList.driverName}}</view> -->
			<uni-table border stripe emptyText="暂无更多数据" >
				<!-- 表头行 -->
				<uni-tr>
					<uni-th align="center" width="200rpx">姓名</uni-th>
					<uni-th align="center" width="100rpx">总数</uni-th>
					<uni-th align="center" width="100rpx">剩余</uni-th>
					<uni-th align="center">操作</uni-th>
				</uni-tr>
				<!-- 表格数据行 -->
				<uni-tr v-for="(item,index) in dataList " :key="index">
					<uni-td>{{item.driverName}}</uni-td>
					<uni-td>{{item.trip}}</uni-td>
					<uni-td>{{item.remainder}}</uni-td>
					<uni-td>
						<view class="display" v-if="item.captain!==1">
							<view style="margin-right: 10rpx; width: 100%;">
								<!-- <up-button @click="add" color="black"> -->
									<up-icon name="plus-square-fill" size="28" labelPos="bottom " @click="add(index)"/>
								<!-- </up-button> -->
							</view>
							<view style="width: 100%;">
								<!-- <up-button @click="subtract" color="black"> -->
									<up-icon name="minus-square-fill" size="28" @click="subtract(index)"/>
								<!-- </up-button> -->
							</view>
						</view>
					</uni-td>
				</uni-tr>
			</uni-table>
		</view>
		
</template>

<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app';
import { ref,reactive } from 'vue';
import { getTotalListApi , addSubtractApi} from '@/apis/assigntasks';
	let captainIndex:string
	const dataList = ref()
	let id: any
	const data = reactive<{taskCarId:number,value:number}>({
		taskCarId:0,
		value:0
	})
	onLoad((e)=>{
		id = e?.id
		getTotalList(id)
	})
	
	function add(index){
		data.taskCarId = dataList.value[index].id
		data.value = +1
		if(dataList.value[captainIndex].remainder!==0){
			addSubtractApi(data).then(()=>{
					dataList.value[index].trip++
					dataList.value[index].remainder++
					dataList.value[captainIndex].trip--
					dataList.value[captainIndex].remainder--
			})
		}else{
			uni.showToast({
				title: '可分配额度为0',
				duration: 1500,
				icon: 'fail'
			});
		}
	}
	function subtract(index){
		data.taskCarId = dataList.value[index].id
		data.value = -1
		if(dataList.value[index].remainder!==0){
			addSubtractApi(data).then(()=>{
				dataList.value[index].trip--
				dataList.value[captainIndex].trip++
				dataList.value[captainIndex].remainder++
				dataList.value[index].remainder--
			})
		}else{
			uni.showToast({
				title: '可分配额度为0',
				duration: 3000
			});
		}
	}
	function getTotalList(id){
		getTotalListApi(id).then((res)=>{
			dataList.value = res
			for(const index in dataList.value){
				if(dataList.value[index].captain === 1){
					captainIndex = index
					break
				}
			}
		})
	
	}
</script>

<style>
.display{
	display: flex;
	flex-wrap: nowrap;
	justify-content: space-between;
}
.table{
	padding: 20rpx;
	height: 100%;
	background: linear-gradient(to bottom, #edd9fd 10% ,#d8c6e6 20%, #F8F8F8 100%);
}
</style>
